﻿using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using Microsoft.Security.Application;

namespace Job.Core.Web
{
    /// <summary>
    /// 与 web 相关的 String 扩展。
    /// </summary>
    public static class WebStringExtensions
    {
        #region Properties
        private static readonly Regex htmlTagRegex = new Regex("<[^<>]*>", RegexOptions.Compiled | RegexOptions.Multiline);
        #endregion

        #region Methods

        /// <summary>
        /// Html 编码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>编码后的字串。</returns>
        public static string HtmlEncode(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;
            s = s.Replace("&", "&amp;");
            s = s.Replace("<", "&lt;");
            s = s.Replace(">", "&gt;");
            s = s.Replace("\"", "&quot;");
            s = s.Replace("\'", "&#39;");
            return HttpUtility.HtmlEncode(s);
        }

        /// <summary>
        /// Html 解码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>解码后的字串。</returns>
        public static string HtmlDecode(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;
            s = s.Replace("&amp;", "&");
            s = s.Replace("&lt;", "<");
            s = s.Replace("&gt;", ">");
            s = s.Replace("&quot;", "\"");
            s = s.Replace("&#39;", "'");
            return HttpUtility.HtmlDecode(s);
        }

        /// <summary>
        /// Url 编码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>编码后的字串。</returns>
        public static string UrlEncode(this string s)
        {
            return HttpUtility.UrlEncode(s);
        }

        /// <summary>
        /// Url 解码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>解码后的字串。</returns>
        public static string UrlDecode(this string s)
        {
            return HttpUtility.UrlDecode(s);
        }

        /// <summary>
        /// 清除 Html 标记。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>清除标记后的字串。</returns>
        public static string CleanHtmlTags(this string s)
        {            
            return htmlTagRegex.Replace(s, string.Empty);
        }

        /// <summary>
        /// 从左开始截取指定长度字符。
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len">要截取的长度。</param>
        /// <returns>截取后的字串。</returns>
        public static string Left(this string s, int len)
        {
            if (s.Length <= len)
                return s;

            return s.Substring(0, len);
        }

        /// <summary>
        /// 从左开始截取指定长度字符，当字符串超过长度时，加'..'号。
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len">要截取的长度。</param>
        /// <returns>截取后的字串。</returns>
        public static string LeftX(this string s, int len)
        {
            if (s.Length <= len)
                return s;

            return s.Substring(0, len) + "..";
        }

        /// <summary>
        /// 将换行符转换为 Html 的 br 标记。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换后的字串。</returns>
        public static string BrEncode(this string s)
        {
            s = s.HtmlEncode();
            s = s.Replace("\r\n", "<br />");
            s = s.Replace("\r", "<br />");
            s = s.Replace("\n", "<br />");
            return s;
        }

        /// <summary>
        /// 将 Html 中的 br 标记转换成换行符。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换后的字串。</returns>
        public static string BrDecode(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;

            return s.Replace("<br />", "\n");
        }

        /// <summary>
        /// 获取安全的 html。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>安全的 html。</returns>
        public static string GetSafeHtml(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;

            return AntiXss.GetSafeHtmlFragment(s);
        }
        #endregion

        #region 字符格式转换        
        /// <summary>
        /// 文本编码
        ///</summary>
        public static string TextEncode(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;

            StringBuilder sb = new StringBuilder(s);
            sb.Replace("&", "&amp;");
            sb.Replace("<", "&lt;");
            sb.Replace(">", "&gt;");
            sb.Replace("\"", "&quot;");
            sb.Replace("\'", "&#39;");
            sb.Replace("\r", "<br/>");
            sb.Replace("\r\n", "<br/>");
            return sb.ToString();
        }
        
        /// <summary>
        /// TEXT解码
        /// </summary>
        public static string TextDecode(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;

            StringBuilder sb = new StringBuilder(s);
            sb.Replace("&amp;", "&");
            sb.Replace("&lt;", "<");
            sb.Replace("&gt;", ">");
            sb.Replace("&quot;", "\"");
            sb.Replace("&#39;", "\'");
            sb.Replace("<br/>", "\r");
            sb.Replace("<br/>", "\r\n");

            //s = CRegex.Replace(s, @"<br\s*>", "\r\n", 0);
            //s = CRegex.Replace(s, @"<br\s*/\s*>", "\r\n", 0);
            //StringBuilder sb = new StringBuilder(s);
            //sb.Replace("<br/><br/>", "\r\n");
            //sb.Replace("<br/>", "\r");
            //sb.Replace("<p></p>", "\r\n\r\n");
            return sb.ToString();
        }


        /// <summary>
        /// 清除html字符
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string CleanHtml(this string text)
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text)) return null;

            text = Regex.Replace(text, "(\\s*<[b|B][r|R]/*>\\s*)+", "\n");	//<br>
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");	//&nbsp;
            text = Regex.Replace(text, "((<>)*[<|>]/*)+", string.Empty);	//<>

            //return CRegex.Replace(str, @"<[^<]+>", "", 0);

            return string.IsNullOrEmpty(text) ? null : text;
        }

        /// <summary>
        /// 替换所有回车换行符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceCRLF(this string str)
        {
            str = str.Replace((char)13, (char)0).Replace((char)10, (char)0).Replace("\0", "");
            return str;
        }


        /// <summary>
        /// UBBHelper代码处理函数
        /// 网络上流传的 UBB编辑器
        /// </summary>
        /// <param name="sDetail">输入字符串</param>
        /// <returns>输出字符串</returns>
        public static string UBBToHTMLAds(this string strUBB)
        {
            Regex r;
            Match m;

            #region 处理空格

            strUBB = strUBB.Replace(" ", "&nbsp;");

            #endregion

            #region 处理单引号

            //strUBB = strUBB.Replace("'", "’");

            #endregion

            #region 处理双引号

            strUBB = strUBB.Replace("\"", "&quot;");

            #endregion

            #region html标记符

            strUBB = strUBB.Replace("<", "&lt;");
            strUBB = strUBB.Replace(">", "&gt;");

            #endregion

            #region 处理换行

            //处理换行，在每个新行的前面添加两个全角空格
            r = new Regex(@"(\r\n((&nbsp;)|　)+)(?<正文>\S+)", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<BR>　　" + m.Groups["正文"].ToString());
            }
            //处理换行，在每个新行的前面添加两个全角空格
            strUBB = strUBB.Replace("\r\n", "<br/>");

            #endregion

            #region 处[b][/b]标记

            r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2].ToString() + "</B>");
            }

            #endregion

            #region 处[i][/i]标记

            r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2].ToString() + "</I>");
            }

            #endregion

            #region 处[u][/u]标记

            r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2].ToString() + "</U>");
            }

            #endregion

            #region 处[p][/p]标记

            //处[p][/p]标记
            r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
            }

            #endregion

            #region 处[sup][/sup]标记

            //处[sup][/sup]标记
            r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<SUP>" + m.Groups[2].ToString() + "</SUP>");
            }

            #endregion

            #region 处[sub][/sub]标记

            //处[sub][/sub]标记
            r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<SUB>" + m.Groups[2].ToString() + "</SUB>");
            }

            #endregion

            #region 处[url][/url]标记

            //处[url][/url]标记
            r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">"
                                        + m.Groups[2].ToString() + "</A>");
            }

            #endregion

            #region 处[url=xxx][/url]标记

            //处[url=xxx][/url]标记
            r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">"
                                        + m.Groups[3].ToString() + "</A>");
            }

            #endregion

            #region 处[email][/email]标记

            //处[email][/email]标记
            r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                                        m.Groups[2].ToString() + "</A>");
            }

            #endregion

            #region 处[email=xxx][/email]标记

            //处[email=xxx][/email]标记
            r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                                        m.Groups[3].ToString() + "</A>");
            }

            #endregion

            #region 处[size=x][/size]标记

            //处[size=x][/size]标记
            r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<FONT SIZE=" + m.Groups[2].ToString() + ">" +
                                        m.Groups[3].ToString() + "</FONT>");
            }

            #endregion

            #region 处[color=x][/color]标记

            //处[color=x][/color]标记
            r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<FONT COLOR=" + m.Groups[2].ToString() + ">" +
                                        m.Groups[3].ToString() + "</FONT>");
            }

            #endregion

            #region 处[font=x][/font]标记

            //处[font=x][/font]标记
            r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<FONT FACE=" + m.Groups[2].ToString() + ">" +
                                        m.Groups[3].ToString() + "</FONT>");
            }

            #endregion

            #region 处理图片链接

            //处理图片链接
            r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID="
                                        + m.Groups[1].ToString() +
                                        "\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1].ToString() +
                                        "\"></A>");
            }

            #endregion

            #region 处理[align=x][/align]

            //处理[align=x][/align]
            r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<P align=" + m.Groups[2].ToString() + ">" +
                                        m.Groups[3].ToString() + "</P>");
            }

            #endregion

            #region 处[H=x][/H]标记

            //处[H=x][/H]标记
            r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<H" + m.Groups[2].ToString() + ">" +
                                        m.Groups[3].ToString() + "</H" + m.Groups[2].ToString() + ">");
            }

            #endregion

            #region 处理[list=x][*][/list]

            //处理[list=x][*][/list]
            r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                string strLI = m.Groups[5].ToString();
                Regex rLI = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
                Match mLI;
                for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
                {
                    strLI = strLI.Replace(mLI.Groups[0].ToString(), "<LI>" + mLI.Groups[1]);
                }
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<UL TYPE=\"" + m.Groups[3].ToString() + "\"><B>" + m.Groups[4].ToString() + "</B>" +
                                        strLI + "</UL>");
            }

            #endregion

            #region 处[SHADOW=x][/SHADOW]标记

            //处[SHADOW=x][/SHADOW]标记
            r = new Regex(@"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/SHADOW\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB =
                    strUBB.Replace(m.Groups[0].ToString(),
                                   "<TABLE WIDTH=" + m.Groups[2].ToString() + "  STYLE=FILTER:SHADOW(COLOR=" + m.Groups[3].ToString() + ", STRENGTH=" + m.Groups[4].ToString() + ")>" +
                                   m.Groups[5].ToString() + "</TABLE>");
            }

            #endregion

            #region 处[glow=x][/glow]标记

            //处[glow=x][/glow]标记
            r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<TABLE WIDTH=" + m.Groups[2].ToString() + "  STYLE=FILTER:GLOW(COLOR=" + m.Groups[3].ToString() + ", STRENGTH=" + m.Groups[4].ToString() + ")>" +
                                        m.Groups[5].ToString() + "</TABLE>");
            }

            #endregion

            #region 处[center][/center]标记

            r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<CENTER>" + m.Groups[2].ToString() + "</CENTER>");
            }

            #endregion

            #region 处[IMG][/IMG]标记

            r = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \S\t]*?)(\[\/IMG\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB =
                    strUBB.Replace(m.Groups[0].ToString(),
                                   "<br><a onfocus=this.blur() href=" + m.Groups[2].ToString() + "://" + m.Groups[3].ToString() + " target=_blank><IMG SRC=" + m.Groups[2].ToString() +
                                   "://" + m.Groups[3].ToString() + " border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
            }

            #endregion

            #region 处[em]标记

            r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<img src=pic/em" + m.Groups[2].ToString() + ".gif border=0 align=middle>");
            }

            #endregion

            #region 处[flash=x][/flash]标记

            //处[mp=x][/mp]标记
            r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<a href=" + m.Groups[4].ToString() +
                                        " TARGET=_blank><IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" +
                                        m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><PARAM NAME=movie VALUE=" + m.Groups[4].ToString() +
                                        "><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=" + m.Groups[4].ToString() +
                                        " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" +
                                        m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + ">" + m.Groups[4].ToString() + "</embed></OBJECT>");
            }

            #endregion

            #region 处[dir=x][/dir]标记

            //处[dir=x][/dir]标记
            r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" +
                                        m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><param name=src value=" + m.Groups[4].ToString() + "><embed src=" +
                                        m.Groups[4].ToString() + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + m.Groups[2].ToString() + " height=" +
                                        m.Groups[3].ToString() + "></embed></object>");
            }

            #endregion

            #region 处[rm=x][/rm]标记

            //处[rm=x][/rm]标记
            r = new Regex(@"(\[rm=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[2].ToString() + " height=" +
                                        m.Groups[3].ToString() + "><PARAM NAME=SRC VALUE=" + m.Groups[4].ToString() +
                                        "><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" +
                                        m.Groups[2].ToString() + "><PARAM NAME=SRC VALUE=" + m.Groups[4].ToString() +
                                        "><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
            }

            #endregion

            #region 处[mp=x][/mp]标记

            //处[mp=x][/mp]标记
            r = new Regex(@"(\[mp=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + m.Groups[2].ToString() +
                                        " height=" + m.Groups[3].ToString() + " ><param name=ShowStatusBar value=-1><param name=Filename value=" + m.Groups[4].ToString() +
                                        "><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" +
                                        m.Groups[4].ToString() + "  width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "></embed></object>");
            }

            #endregion

            #region 处[qt=x][/qt]标记

            //处[qt=x][/qt]标记
            r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<embed src=" + m.Groups[4].ToString() + " width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() +
                                        " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
            }

            #endregion

            #region 处[QUOTE][/QUOTE]标记

            r = new Regex(@"(\[QUOTE\])([ \S\t]*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB =
                    strUBB.Replace(m.Groups[0].ToString(),
                                   "<table cellpadding=0 cellspacing=0 border=1 WIDTH=94% bordercolor=#000000 bgcolor=#F2F8FF align=center  style=FONT-SIZE: 9pt><tr><td  ><table width=100% cellpadding=5 cellspacing=1 border=0><TR><TD >" +
                                   m.Groups[2].ToString() + "</table></table><br>");
            }

            #endregion

            #region 处[move][/move]标记

            r = new Regex(@"(\[move\])([ \S\t]*?)(\[\/move\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
            }

            #endregion

            #region 处[FLY][/FLY]标记

            r = new Regex(@"(\[FLY\])([ \S\t]*?)(\[\/FLY\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
            }

            #endregion

            #region 处[image][/image]标记

            //处[image][/image]标记
            r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
            for (m = r.Match(strUBB); m.Success; m = m.NextMatch())
            {
                strUBB = strUBB.Replace(m.Groups[0].ToString(),
                                        "<img src=\"" + m.Groups[2].ToString() + "\" border=0 align=middle><br>");
            }

            #endregion

            return strUBB;
        }
        /*
        public static string UBBToHTML(this string sDetail)
        {
            Regex r;
            Match m;
            #region 处理空格
            //sDetail = sDetail.Replace(" ", "&nbsp;");
            #endregion
            #region 处理单引号
            sDetail = sDetail.Replace("'", "’");
            #endregion
            #region 处理双引号
            sDetail = sDetail.Replace("\"", "&quot;");
            #endregion
            #region 处理html标记符
            sDetail = sDetail.Replace("<", "&lt;");
            sDetail = sDetail.Replace(">", "&gt;");

            #endregion
            #region 处理换行
            //处理换行，在每个新行的前面添加两个全角空格
            r = new Regex(@"(\r\n((&nbsp;)|　)+)(?<正文>\S+)", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<BR>　　" + m.Groups["正文"].ToString());
            }
            //处理换行，在每个新行的前面添加两个全角空格
            sDetail = sDetail.Replace("\r\n", "<BR>");
            sDetail = sDetail.Replace("\n", "<BR>");
            #endregion
            #region 处理[b][/b]标记
            r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2].ToString() + "</B>");
            }
            #endregion
            #region 处理[i][/i]标记
            r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2].ToString() + "</I>");
            }
            #endregion
            #region 处理[u][/u]标记
            r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2].ToString() + "</U>");
            }
            #endregion
            #region 处理[p][/p]标记
            //处理[p][/p]标记
            r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
            }
            #endregion
            #region 处理[sup][/sup]标记
            //处理[sup][/sup]标记
            r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<SUP>" + m.Groups[2].ToString() + "</SUP>");
            }
            #endregion
            #region 处理[sub][/sub]标记
            //处理[sub][/sub]标记
            r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<SUB>" + m.Groups[2].ToString() + "</SUB>");
            }
            #endregion
            #region 处理标记
            //处理标记
            r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">"
            + m.Groups[2].ToString() + "</A>");
            }
            #endregion
            #region 处理[url=xxx][/url]标记
            //处理[url=xxx][/url]标记
            r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">"
            + m.Groups[3].ToString() + "</A>");
            }
            #endregion
            #region 处理[email][/email]标记
            //处理[email][/email]标记
            r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                 m.Groups[2].ToString() + "</A>");
            }
            #endregion
            #region 处理[email=xxx][/email]标记
            //处理[email=xxx][/email]标记
            r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                 m.Groups[3].ToString() + "</A>");
            }
            #endregion
            #region 处理[size=x][/size]标记
            //处理[size=x][/size]标记
            r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<FONT SIZE=" + m.Groups[2].ToString() + ">" +
                 m.Groups[3].ToString() + "</FONT>");
            }
            #endregion
            #region 处理[color=x][/color]标记
            //处理[color=x][/color]标记
            r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<FONT COLOR=" + m.Groups[2].ToString() + ">" +
                 m.Groups[3].ToString() + "</FONT>");
            }
            #endregion
            #region 处理[font=x][/font]标记
            //处理[font=x][/font]标记
            r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<FONT FACE=" + m.Groups[2].ToString() + ">" +
                 m.Groups[3].ToString() + "</FONT>");
            }
            #endregion
            #region 处理图片链接
            //处理图片链接
            r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID="
            + m.Groups[1].ToString() +
                 "\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1].ToString() +
                 "\"></A>");
            }
            #endregion
            #region 处理[align=x][/align]
            //处理[align=x][/align]
            r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<P align=" + m.Groups[2].ToString() + ">" +
                 m.Groups[3].ToString() + "</P>");
            }
            #endregion
            #region 处理[H=x][/H]标记
            //处理[H=x][/H]标记
            r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<H" + m.Groups[2].ToString() + ">" +
                 m.Groups[3].ToString() + "</H" + m.Groups[2].ToString() + ">");
            }
            #endregion
            #region 处理[list=x][*][/list]
            //处理[list=x][*][/list]
            r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                string strLI = m.Groups[5].ToString();
                Regex rLI = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
                Match mLI;
                for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
                {
                    strLI = strLI.Replace(mLI.Groups[0].ToString(), "<LI>" + mLI.Groups[1]);
                }
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<UL TYPE=\"" + m.Groups[3].ToString() + "\"><B>" + m.Groups[4].ToString() + "</B>" +
                 strLI + "</UL>");
            }

            #endregion
            #region 处理[SHADOW=x][/SHADOW]标记
            //处理[SHADOW=x][/SHADOW]标记
            r = new Regex(@"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/SHADOW\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<TABLE WIDTH=" + m.Groups[2].ToString() + "  STYLE=FILTER:SHADOW(COLOR=" + m.Groups[3].ToString() + ", STRENGTH=" + m.Groups[4].ToString() + ")>" +
                 m.Groups[5].ToString() + "</TABLE>");
            }
            #endregion
            #region 处理[glow=x][/glow]标记
            //处理[glow=x][/glow]标记
            r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<TABLE WIDTH=" + m.Groups[2].ToString() + "  STYLE=FILTER:GLOW(COLOR=" + m.Groups[3].ToString() + ", STRENGTH=" + m.Groups[4].ToString() + ")>" +
                 m.Groups[5].ToString() + "</TABLE>");
            }
            #endregion
            #region 处理[center][/center]标记
            r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<CENTER>" + m.Groups[2].ToString() + "</CENTER>");
            }
            #endregion
            #region 处理标记
            r = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \S\t]*?)(\[\/IMG\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<br><a onfocus=this.blur() href=" + m.Groups[2].ToString() + "://" + m.Groups[3].ToString() + " target=_blank><IMG SRC=" + m.Groups[2].ToString() + "://" + m.Groups[3].ToString() + " border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
            }
            #endregion
            #region 处理[em]标记
            r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<img src=pic/em" + m.Groups[2].ToString() + ".gif border=0 align=middle>");
            }
            #endregion
            #region 处理[flash=x][/flash]标记
            //处理[mp=x][/mp]标记
            r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<a href=" + m.Groups[4].ToString() + " TARGET=_blank& gt;<IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]< /a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><PARAM NAME=movie VALUE=" + m.Groups[4].ToString() + "><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=" + m.Groups[4].ToString() + " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + ">" + m.Groups[4].ToString() + "</embed></OBJECT>");
            }
            #endregion
            #region 处理[dir=x][/dir]标记
            //处理[dir=x][/dir]标记
            r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><param name=src value=" + m.Groups[4].ToString() + "><embed src=" + m.Groups[4].ToString() + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "></embed></object>");
            }
            #endregion
            #region 处理[rm=x][/rm]标记
            //处理[rm=x][/rm]标记
            r = new Regex(@"(\[rm=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><PARAM NAME=SRC VALUE=" + m.Groups[4].ToString() + "><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + m.Groups[2].ToString() + "><PARAM NAME=SRC VALUE=" + m.Groups[4].ToString() + "><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
            }
            #endregion
            #region 处理[mp=x][/mp]标记
            //处理[mp=x][/mp]标记
            r = new Regex(@"(\[mp=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + " ><param name=ShowStatusBar value=-1><param name=Filename value=" + m.Groups[4].ToString() + "><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" + m.Groups[4].ToString() + "  width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "></embed></object>");
            }
            #endregion
            #region 处理[qt=x][/qt]标记
            //处理[qt=x][/qt]标记
            r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<embed src=" + m.Groups[4].ToString() + " width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
            }
            #endregion
            #region 处理[QUOTE][/QUOTE]标记
            r = new Regex(@"(\[QUOTE\])([ \S\t]*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<table cellpadding=0 cellspacing=0 border=1 WIDTH=94% bordercolor=#000000 bgcolor=#F2F8FF align=center  style=FONT-SIZE: 9pt><tr><td  ><table width=100% cellpadding=5 cellspacing=1 border=0><TR><TD >" + m.Groups[2].ToString() + "</table></table><br>");
            }
            #endregion
            #region 处理[move][/move]标记
            r = new Regex(@"(\[move\])([ \S\t]*?)(\[\/move\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
            }
            #endregion
            #region 处理[FLY][/FLY]标记
            r = new Regex(@"(\[FLY\])([ \S\t]*?)(\[\/FLY\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
            }
            #endregion
            #region 处理[image][/image]标记
            //处理[image][/image]标记
            r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<img src=\"" + m.Groups[2].ToString() + "\" border=0 align=middle><br>");
            }
            #endregion

            return sDetail;
        }*/
        #endregion
    }
}